(function () {
    'use strict';

    angular.module('app').controller('wct.template', WctTemplateCtrl);

    WctTemplateCtrl.$inject = [
        '$scope',
        '$win',
        '$toast',
        'Api',
        'WctTemplateService'
    ];
    function WctTemplateCtrl($scope, $win, $toast, Api, WctTemplateService) {
        $scope.params = {};
        $scope.options = {
            api     : Api.wct.template.page,
            checkbox: true,
            column  : [{
                text : '序号',
                index: true
            }, {
                text : '标题',
                name : 'title',
                ratio: 3 //列宽比列, 不配置时默认1
            }, {
                text    : '模版类型',
                name    : 'type',
                renderer: function (row) {
                    if (row.type === 'text') {
                        return '文本';
                    } else if (row.type === 'image') {
                        return '图片';
                    } else if (row.type === 'voice') {
                        return '声音';
                    } else if (row.type === 'video') {
                        return '视频';
                    } else {
                        return '其他';
                    }
                }
            }, {
                text : '内容',
                name : 'content',
                ratio: 5
            }, {
                name : '操作列',
                type : 'action',
                items: [{
                    icon   : 'fa fa-comments',
                    tooltip: '发送消息',
                    handler: function (o, row) {
                        console.log(row);
                    }
                }, {
                    icon   : 'fa fa-tasks',
                    tooltip: '详情',
                    handler: function (o, row) {
                        console.log(row);
                    }
                }]
            }]
        };

        resetTemplate();
        $scope.types = {
            '文本': 'text',
            '图片': 'image',
            '声音': 'voice',
            '视频': 'video'
        };
        $scope.addTemplate = function () {
            dialogShow();
        };

        function dialogShow(data) {
            $win.open({
                template: 'templateDialog',
                data    : data,
                scope   : $scope
            });
        }

        $scope.submit = function () {
            WctTemplateService.save($scope.template)
                .then(function (response) {
                    console.log('success => ', response);
                }, function (response) {
                    console.log('error => ', response);
                }, function (evt) {
                    console.log(evt);
                });
        };

        function resetTemplate() {
            $scope.template = {
                type: 'text'
            };
        }

        $scope.search = function (name) {
            $scope.params.title = name;
        };
    }
})();